package com.yjyw.tiexie.common;

import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Looper;

import com.yjyw.tiexie.util.LogUtil;

import java.lang.reflect.Field;

public class OnFront {
    public static long ON_FRONT_TIME;

    public static OnFront getInstance() {
        return InnerClass.instance;
    }

    public void onFront() {
        LogUtil.e("OnFront", "前后台：App回到前台");
        GlobalConstant.isFront = true;
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                doSomeThing();
            }
        });
    }

    private void doSomeThing() {
        ON_FRONT_TIME = System.currentTimeMillis();

        //防止开发者选项关闭动画后使其失效
//        resetAnimatorDurationScale();
    }

    private void resetAnimatorDurationScale() {
        try {
            Field field = ValueAnimator.class.getDeclaredField("sDurationScale");
            field.setAccessible(true);
            if (field.getFloat(null) == 0) {
                field.setFloat(null, 1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static class InnerClass {
        @SuppressLint("StaticFieldLeak")
        private static OnFront instance = new OnFront();
    }
}
